var express = require('express');
var router = express.Router();
const { ShopModel, OrderModel, FriendModel, MessageModel } = require('../model/yzb_model')


router.get('/getdata', async (req, res) => {
    res.send({ data: 123 })
})

// 添加好友
router.post('/insertFriend', async (req, res) => {
    let data = req.body
    console.log(data);
    let body = {
        userId: data.uid,
        friendId: data.fid,
        time: Date.now(),
        lastTime: Date.now()
    }
    // console.log(body);
    await FriendModel.create(body)
    res.send({
        code: 200,
        msg: "添加成功",
    })
})

// 添加一对一消息
router.post('/insertMsg', async (req, res) => {
    let data = req.body
    let body = {
        userId: data.uid,
        friendId: data.fid,
        message: data.msg,
        types: data.types,
        time: Date.now(),
        state: 1
    }
    // console.log(body);
    await MessageModel.create(body)
    res.send({
        code: 200,
        msg: "添加成功",
    })
})

// 好友最后通讯时间
router.post("/updatetime", async (req, res) => {
    let data = req.body
    let wherestr = { userId: data.uid, friendId: data.fid }
    let updatestr = { lastTime: Date.now() }
    await FriendModel.updateOne(wherestr, { $set: updatestr })
    res.send({
        msg: "修改成功",
        code: 200
    })
})



//获取最后一条消息
router.post('/getlastmsg', async (req, res) => {
    let data = req.body
    let query = await MessageModel.findOne({ $or: [{ 'userId': data.uid, "friendId": data.fid }, { 'userId': data.fid, "friendId": data.uid }] }).sort({ time: -1 })
    res.send({
        query,
        code: 200,
        msg: "查询成功",
    })
})

// 汇总一对一消息未读数
router.post('/unreadMsg', async (req, res) => {
    let data = req.body
    let wherestr = { userId: data.uid, friendId: data.fid, state: 1 }  //未读
    let query = await MessageModel.find(wherestr).count()
    res.send({
        query,
        code: 200,
        msg: "查询成功",
    })
})

// 一对一消息状态修改（已读）
router.post("/updateMsg", async (req, res) => {
    let data = req.body
    let wherestr = { userId: data.uid, friendId: data.fid, state: 1 }
    let updatestr = { state: 0 }
    await MessageModel.update(wherestr, { $set: updatestr })
    res.send({
        msg: "修改成功",
        code: 200
    })
})

// 获取好友列表
router.post('/getfreind', async (req, res) => {
    let data = req.body
    let arr = await FriendModel.find({ userId: data.uid }).sort({ time: -1 }).lean().populate('friendId')
    let query = arr.map(item => {
        return {
            id: item.friendId._id,
            name: item.friendId.name, // 用户表里的姓名
            avater: item.friendId.avater, // 用户表里的头像
            lastTime: item.lastTime
        }
    })
    // console.log(arr);
    res.send({
        query,
        code: 200,
        msg: "查询成功",
    })
})

// 消息操作
// 获取一对一聊天数据
router.post('/msg', async (req, res) => {
    let data = req.body
    let arr = await MessageModel.find({ $or: [{ 'userId': data.uid, "friendId": data.fid }, { 'userId': data.fid, "friendId": data.uid }] }).sort({ time: -1 }).lean().populate('userId')
    let query = arr.map(item => {
        return {
            id: item._id,
            message: item.message,
            types: item.types,
            time: item.time,
            fromId: item.userId._id, // 谁发的消息
            imgurl: item.userId.avater, // 用户表里的头像
        }
    })
    // console.log(arr);
    res.send({
        query,
        code: 200,
        msg: "查询成功",
    })
})

// 门店管理获取数据
router.get('/getshop', async (req, res) => {
    let all = await ShopModel.find()
    let list = await ShopModel.find()
    res.send({
        code: 200,
        data: list,
        all,
    })
});


// 门店管理添加
router.post('/shopAdd', async (req, res) => {
    console.log('进来了');
    console.log(req.body);
    let body = req.body.objs;
    console.log(body);
    let data = await ShopModel.create(body);
    res.send({
        code: 200,
        msg: '添加成功',
        data
    })
})

// 接单管理添加
router.post('/orderAdd', async (req, res) => {
    console.log('进来了');
    let body = req.body.objs;
    console.log(body);
    let data = await OrderModel.create(body);
    res.send({
        code: 200,
        msg: '添加成功',
        data
    })
})

module.exports = router;